//
// SoapTrace.Net - A soap message monitoring utility. 
//
// Copyright (C) 2005 Matthew Ward
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
// Matthew Ward (mrward@users.sourceforge.net)

using NUnit.Framework;
using SoapTrace.Core.Communication;
using System;
using System.Text;

namespace SoapTrace.Tests
{
	/// <summary>
	/// Tests the <see cref="SoapHeader.Regenerate"/> method.
	/// </summary>
	[ TestFixture ]
	public class SoapHeaderTestFixture
	{
		/// <summary>
		/// Check a different IP address, but same port creates
		/// a different client id.
		/// </summary>
		[ Test ]
		public void ContentLengthChanged( )
		{
			SoapRequestHeader Header = CreateSoapHeader( GetHeaderText( ) );
		
			SoapHeaderField Field = Header.Fields.Find( SoapHeader.ContentLengthFieldName );
			Field.Value = "1000";
			Header.Regenerate( );
			
			Assert.AreEqual( GetExpectedHeaderText( ), 
			                Header.Header,
			                "Unexpected header text." );
		}
		
		SoapRequestHeader CreateSoapHeader( string text )
		{
			SoapRequestHeader RequestHeader = new SoapRequestHeader( );
			
			bool FoundHeader = false;
			
			byte[ ] Bytes = UnicodeEncoding.UTF8.GetBytes( text );
			
			foreach( byte CurrentByte in Bytes )
			{
				FoundHeader = RequestHeader.AddByte( CurrentByte );
				
				if( FoundHeader )
				{
					break;
				}
			}
			
			if( FoundHeader == false )
			{
				throw new ApplicationException( "Could not parse header." );
			}
			
			return RequestHeader;
		}
		
		string GetHeaderText( )
		{
			return "POST /TextEJB.asmx HTTP/1.1\r\n" +
				"User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; MS Web Services Client Protocol 1.1.4322.573)\r\n" +
				"Content-Type: text/xml; charset=utf-8\r\n" +
				"SOAPAction: \"urn:CompressList\"\r\n" +
				"Content-Length: 660\r\n" +
				"Expect: 100-continue\r\n" +
				"Connection: Keep-Alive\r\n" +
				"Host: localhost:7001\r\n" +
				"\r\n";
		}
		
		string GetExpectedHeaderText( )
		{
			return "POST /TextEJB.asmx HTTP/1.1\r\n" +
				"User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; MS Web Services Client Protocol 1.1.4322.573)\r\n" +
				"Content-Type: text/xml; charset=utf-8\r\n" +
				"SOAPAction: \"urn:CompressList\"\r\n" +
				"Content-Length: 1000\r\n" +
				"Expect: 100-continue\r\n" +
				"Connection: Keep-Alive\r\n" +
				"Host: localhost:7001\r\n" +
				"\r\n";;
		}

	}
}
